
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Listener2 extends JFrame {

   public Listener2() {
      JButton open = new JButton("Open");
      JButton close = new JButton("Close");
      JButton save = new JButton("Save");
      getContentPane().setLayout(new FlowLayout());
      getContentPane().add(open);
      getContentPane().add(close);
      getContentPane().add(save);

      ActionListener listener = new ButtonAction();
      open.addActionListener(listener);
      close.addActionListener(listener);
      save.addActionListener(listener);

      pack();
      setVisible(true);
   }

   protected void open() {
      System.out.println("Open a file");
   }
   protected void close() {
      System.out.println("Close a file");
   }
   protected void save() {
      System.out.println("Save a file");
   }

   class ButtonAction implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         JButton b = (JButton)e.getSource();
         if ( b.getText().equals("Open") ) {
            open();
         } else if (b.getText().equals("Close")) {
            close();
         } else if (b.getText().equals("Save")) {
            save();
         }

      }
   }


   public static void main(String[] args) {
      new Listener2();
   }

}
